﻿
//------------------------------------------------------------------------------
// 创建标识: Copyright (C) 2009 Socansoft.com 版权所有
// 创建描述: SocanCode代码生成器自动创建于 2009-2-9 8:57:37
//
// 功能描述: 
//
// 修改标识: 
// 修改描述: 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using IDAL;
using DBUtility;
using System.Collections;
using System.Collections.Generic;

namespace SQLServerDAL
{
    /// <summary>
    /// 数据访问类 IntergalTypeSet
    /// </summary>
    public class IntergalTypeSet : IIntergalTypeSet
    {
        public IntergalTypeSet()
        { }

        #region  成员方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public string Add(Model.IntergalTypeSet model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [IntergalTypeSet](");
            strSql.Append("[InergalTypeId],[InergalTypeName],[InergalRadio],[InergalTypeEndDateTime],[IntergalSetExtendAttribute1],[IntergalSetExtendAttribute2],[IntergalSetExtendAttribute3],[IntergalSetExtendAttribute4],[IntergalSetExtendAttribute5],[IntergalSetExtendAttribute6],[IntergalSetExtendAttribute7],[IntergalSetExtendAttribute8],[IntergalSetEffact],[InergalTypeUnit])");
            strSql.Append(" values (");
            strSql.Append("@InergalTypeId,@InergalTypeName,@InergalRadio,@InergalTypeEndDateTime,@IntergalSetExtendAttribute1,@IntergalSetExtendAttribute2,@IntergalSetExtendAttribute3,@IntergalSetExtendAttribute4,@IntergalSetExtendAttribute5,@IntergalSetExtendAttribute6,@IntergalSetExtendAttribute7,@IntergalSetExtendAttribute8,@IntergalSetEffact,@InergalTypeUnit)");
            SqlParameter[] parameters = {
                    new SqlParameter("@InergalTypeId", SqlDbType.VarChar,24),
                    new SqlParameter("@InergalTypeName", SqlDbType.VarChar,50),
                    new SqlParameter("@InergalRadio", SqlDbType.Decimal),
                    new SqlParameter("@InergalTypeEndDateTime", SqlDbType.Decimal),
                    new SqlParameter("@IntergalSetExtendAttribute1", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalSetExtendAttribute2", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalSetExtendAttribute3", SqlDbType.VarChar,100),
                    new SqlParameter("@IntergalSetExtendAttribute4", SqlDbType.VarChar,100),
                    new SqlParameter("@IntergalSetExtendAttribute5", SqlDbType.Int),
                    new SqlParameter("@IntergalSetExtendAttribute6", SqlDbType.Int),
                    new SqlParameter("@IntergalSetExtendAttribute7", SqlDbType.Decimal),
                    new SqlParameter("@IntergalSetExtendAttribute8", SqlDbType.Decimal),
                    new SqlParameter("@IntergalSetEffact",SqlDbType.DateTime),
                    new SqlParameter("@InergalTypeUnit", SqlDbType.VarChar,50)
            };
            parameters[0].Value = model.InergalTypeId;

            if (model.InergalTypeName != null)
                parameters[1].Value = model.InergalTypeName;
            else
                parameters[1].Value = DBNull.Value;

            parameters[2].Value = model.InergalRadio;
            parameters[3].Value = model.InergalTypeEndDateTime;

            if (model.IntergalSetExtendAttribute1 != null)
                parameters[4].Value = model.IntergalSetExtendAttribute1;
            else
                parameters[4].Value = DBNull.Value;


            if (model.IntergalSetExtendAttribute2 != null)
                parameters[5].Value = model.IntergalSetExtendAttribute2;
            else
                parameters[5].Value = DBNull.Value;


            if (model.IntergalSetExtendAttribute3 != null)
                parameters[6].Value = model.IntergalSetExtendAttribute3;
            else
                parameters[6].Value = DBNull.Value;


            if (model.IntergalSetExtendAttribute4 != null)
                parameters[7].Value = model.IntergalSetExtendAttribute4;
            else
                parameters[7].Value = DBNull.Value;

            parameters[8].Value = model.IntergalSetExtendAttribute5;
            parameters[9].Value = model.IntergalSetExtendAttribute6;
            parameters[10].Value = model.IntergalSetExtendAttribute7;
            parameters[11].Value = model.IntergalSetExtendAttribute8;

            if (model.IntergalSetEffact != DateTime.MinValue)
                parameters[12].Value = model.IntergalSetEffact;
            else
                parameters[12].Value = DBNull.Value;

            if (model.InergalTypeUnit != null)
                parameters[13].Value = model.InergalTypeUnit;
            else
                parameters[13].Value = DBNull.Value;


            if (SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0)
                return model.InergalTypeId;
            else
                return null;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.IntergalTypeSet model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [IntergalTypeSet] set ");
            strSql.Append("[InergalTypeName]=@InergalTypeName,");
            strSql.Append("[InergalRadio]=@InergalRadio,");
            strSql.Append("[InergalTypeEndDateTime]=@InergalTypeEndDateTime,");
            strSql.Append("[IntergalSetExtendAttribute1]=@IntergalSetExtendAttribute1,");
            strSql.Append("[IntergalSetExtendAttribute2]=@IntergalSetExtendAttribute2,");
            strSql.Append("[IntergalSetExtendAttribute3]=@IntergalSetExtendAttribute3,");
            strSql.Append("[IntergalSetExtendAttribute4]=@IntergalSetExtendAttribute4,");
            strSql.Append("[IntergalSetExtendAttribute5]=@IntergalSetExtendAttribute5,");
            strSql.Append("[IntergalSetExtendAttribute6]=@IntergalSetExtendAttribute6,");
            strSql.Append("[IntergalSetExtendAttribute7]=@IntergalSetExtendAttribute7,");
            strSql.Append("[IntergalSetExtendAttribute8]=@IntergalSetExtendAttribute8,");
            strSql.Append("[IntergalSetEffact]=@IntergalSetEffact,");
            strSql.Append("[InergalTypeUnit]=@InergalTypeUnit");

            strSql.Append(" where [InergalTypeId]=@InergalTypeId");
            SqlParameter[] parameters = {
                    new SqlParameter("@InergalTypeId", SqlDbType.VarChar,24),
                    new SqlParameter("@InergalTypeName", SqlDbType.VarChar,50),
                    new SqlParameter("@InergalRadio", SqlDbType.Decimal),
                    new SqlParameter("@InergalTypeEndDateTime", SqlDbType.Decimal),
                    new SqlParameter("@IntergalSetExtendAttribute1", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalSetExtendAttribute2", SqlDbType.VarChar,50),
                    new SqlParameter("@IntergalSetExtendAttribute3", SqlDbType.VarChar,100),
                    new SqlParameter("@IntergalSetExtendAttribute4", SqlDbType.VarChar,100),
                    new SqlParameter("@IntergalSetExtendAttribute5", SqlDbType.Int),
                    new SqlParameter("@IntergalSetExtendAttribute6", SqlDbType.Int),
                    new SqlParameter("@IntergalSetExtendAttribute7", SqlDbType.Decimal),
                    new SqlParameter("@IntergalSetExtendAttribute8", SqlDbType.Decimal),
                    new SqlParameter("@IntergalSetEffact",SqlDbType.DateTime),
                    new SqlParameter("@InergalTypeUnit", SqlDbType.VarChar,50)
            };
            parameters[0].Value = model.InergalTypeId;

            if (model.InergalTypeName != null)
                parameters[1].Value = model.InergalTypeName;
            else
                parameters[1].Value = DBNull.Value;

            parameters[2].Value = model.InergalRadio;
            parameters[3].Value = model.InergalTypeEndDateTime;

            if (model.IntergalSetExtendAttribute1 != null)
                parameters[4].Value = model.IntergalSetExtendAttribute1;
            else
                parameters[4].Value = DBNull.Value;


            if (model.IntergalSetExtendAttribute2 != null)
                parameters[5].Value = model.IntergalSetExtendAttribute2;
            else
                parameters[5].Value = DBNull.Value;


            if (model.IntergalSetExtendAttribute3 != null)
                parameters[6].Value = model.IntergalSetExtendAttribute3;
            else
                parameters[6].Value = DBNull.Value;


            if (model.IntergalSetExtendAttribute4 != null)
                parameters[7].Value = model.IntergalSetExtendAttribute4;
            else
                parameters[7].Value = DBNull.Value;

            parameters[8].Value = model.IntergalSetExtendAttribute5;
            parameters[9].Value = model.IntergalSetExtendAttribute6;
            parameters[10].Value = model.IntergalSetExtendAttribute7;
            parameters[11].Value = model.IntergalSetExtendAttribute8;

            if (model.IntergalSetEffact != DateTime.MinValue)
                parameters[12].Value = model.IntergalSetEffact;
            else
                parameters[12].Value = DBNull.Value;

            if (model.InergalTypeUnit != null)
                parameters[13].Value = model.InergalTypeUnit;
            else
                parameters[13].Value = DBNull.Value;


            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string InergalTypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete [IntergalTypeSet] ");
            strSql.Append(" where [InergalTypeId]=@InergalTypeId");
            SqlParameter[] parameters = {
                    new SqlParameter("@InergalTypeId", SqlDbType.VarChar,24)};
            parameters[0].Value = InergalTypeId;

            return SqlHelper.ExecuteSql(SqlHelper.LocalSqlServer, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string InergalTypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [IntergalTypeSet]");
            strSql.Append(" where [InergalTypeId]= @InergalTypeId");
            SqlParameter[] parameters = {
                    new SqlParameter("@InergalTypeId", SqlDbType.VarChar,24)};
            parameters[0].Value = InergalTypeId;
            return SqlHelper.Exists(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.IntergalTypeSet GetModel(string InergalTypeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from [IntergalTypeSet] ");
            strSql.Append(" where [InergalTypeId]=@InergalTypeId");
            SqlParameter[] parameters = {
                    new SqlParameter("@InergalTypeId", SqlDbType.VarChar,24)};
            parameters[0].Value = InergalTypeId;
            DataSet ds = SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow r = ds.Tables[0].Rows[0];
                return GetModel(r);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取数据集,建议只在多表联查时使用
        /// </summary>
        public DataSet GetDataSet(string strSql)
        {
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql);
        }

        /// <summary>
        /// 获取泛型数据列表,建议只在多表联查时使用
        /// </summary>
        public IList<Model.IntergalTypeSet> GetList(string strSql)
        {
            return GetList(GetDataSet(strSql));
        }

        /// <summary>
        /// 获取数据集,在单表查询时使用
        /// </summary>
        public DataSet GetDataSet(string strWhat, string strWhere, string strOrderby)
        {
            if (string.IsNullOrEmpty(strWhat))
                strWhat = "*";
            StringBuilder strSql = new StringBuilder("select " + strWhat + " from [IntergalTypeSet]");
            if (!string.IsNullOrEmpty(strWhere))
                strSql.Append(" where " + strWhere);
            if (!string.IsNullOrEmpty(strOrderby))
                strSql.Append(" order by " + strOrderby);
            return SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString());
        }

        /// <summary>
        /// 获取泛型数据列表,在单表查询时使用
        /// </summary>
        public IList<Model.IntergalTypeSet> GetList(string strWhat, string strWhere, string strOrderby)
        {
            return GetList(GetDataSet(strWhat, strWhere, strOrderby));
        }

        /// <summary>
        /// 分页获取泛型数据列表
        /// </summary>
        public IList<Model.IntergalTypeSet> GetList(int pageSize, int pageIndex, string fldSort, bool sort, string strCondition, out int pageCount, out int count)
        {
            string strSql;
            DataSet ds = SqlHelper.PageList(SqlHelper.LocalSqlServer, "[IntergalTypeSet]", null, pageSize, pageIndex, fldSort, sort, strCondition, "InergalTypeId", false, out pageCount, out count, out strSql);
            return GetList(ds);
        }
        #endregion

        /// <summary>
        /// 由一行数据得到一个实体
        /// </summary>
        private Model.IntergalTypeSet GetModel(DataRow r)
        {
            Model.IntergalTypeSet model = new Model.IntergalTypeSet();
            model.InergalTypeId = SqlHelper.GetString(r["InergalTypeId"]);
            model.InergalTypeName = SqlHelper.GetString(r["InergalTypeName"]);
            model.InergalRadio = SqlHelper.GetDecimal(r["InergalRadio"]);
            model.InergalTypeEndDateTime = SqlHelper.GetDecimal(r["InergalTypeEndDateTime"]);
            model.IntergalSetExtendAttribute1 = SqlHelper.GetString(r["IntergalSetExtendAttribute1"]);
            model.IntergalSetExtendAttribute2 = SqlHelper.GetString(r["IntergalSetExtendAttribute2"]);
            model.IntergalSetExtendAttribute3 = SqlHelper.GetString(r["IntergalSetExtendAttribute3"]);
            model.IntergalSetExtendAttribute4 = SqlHelper.GetString(r["IntergalSetExtendAttribute4"]);
            model.IntergalSetExtendAttribute5 = SqlHelper.GetInt(r["IntergalSetExtendAttribute5"]);
            model.IntergalSetExtendAttribute6 = SqlHelper.GetInt(r["IntergalSetExtendAttribute6"]);
            model.IntergalSetExtendAttribute7 = SqlHelper.GetDecimal(r["IntergalSetExtendAttribute7"]);
            model.IntergalSetExtendAttribute8 = SqlHelper.GetDecimal(r["IntergalSetExtendAttribute8"]);
            model.IntergalSetEffact = SqlHelper.GetDateTime(r["IntergalSetEffact"]);
            model.InergalTypeUnit = SqlHelper.GetString(r["InergalTypeUnit"]);
            return model;
        }

        /// <summary>
        /// 由数据集得到泛型数据列表
        /// </summary>
        private IList<Model.IntergalTypeSet> GetList(DataSet ds)
        {
            List<Model.IntergalTypeSet> l = new List<Model.IntergalTypeSet>();
            foreach (DataRow r in ds.Tables[0].Rows)
            {
                l.Add(GetModel(r));
            }
            return l;
        }
    }
}
